package acm.pta13;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;

//dfs
public class 还原文件2 {
    static int[] arr;   //缺口
    static int[][] files;   //字片
    static boolean[] visited = new boolean[102];
    static LinkedList<Integer> res = new LinkedList<>();

    public static void main(String[] args) {
        Reader sc = new Reader();
        int n = sc.nextInt();
        arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        int m = sc.nextInt();
        files = new int[m + 1][];
        for (int i = 1; i < files.length; i++) {
            files[i] = new int[sc.nextInt()];
            for (int j = 0; j < files[i].length; j++) {
                files[i][j] = sc.nextInt();
            }
        }
        dfs(0);
        StringBuilder builder = new StringBuilder();
        res.forEach(i -> builder.append(i).append(" "));
        builder.deleteCharAt(builder.length() - 1);
        System.out.println(builder);
    }

    /**
     * 从缺口的i位置开始找到一条符合的路径
     */
    public static void dfs(int i) {
        for (int j = 1; j < files.length; j++) {
            if (visited[j]) continue;
            if (compare(i, files[j])) {
                visited[j] = true;
                res.add(j);
                dfs(i + files[j].length - 1);
                if (files.length - 1 == res.size()) return;
                res.removeLast();
                visited[j] = false;
            }
        }
    }

    public static boolean compare(int l, int[] file) {
        for (int i = 0; i < file.length; i++) {
            if (i + l >= arr.length || arr[i + l] != file[i]) {
                return false;
            }
        }
        return true;

    }

    static class Reader {
        static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        static StreamTokenizer in = new StreamTokenizer(reader);

        public static int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {
            }
            return (int) in.nval;
        }
    }
}
